package com.example.powernodemall.filter;

import com.example.powernodemall.constant.BaseConstant;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@Component
public class FeignRequestFilter implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (servletRequestAttributes != null) {
            HttpServletRequest request = servletRequestAttributes.getRequest();
            String header = request.getHeader(BaseConstant.AuthHeader);
            if (header != null) {
                requestTemplate.header(BaseConstant.AuthHeader, BaseConstant.ToKen_Prefix+header);
                return;
            }
        }

        /**
         * TODO 当无TOKEN调用OPENFEIGN
         */
    }
}
